<template>
    <div class="main">
        <div class="login-box">
            <h1 style="text-align: center; padding: left 40px; margin-bottom: 20px;padding-left: 60px;">PingMaster</h1>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="captchcode">
                    <el-input v-model.number="ruleForm.captchcode"></el-input>
                </el-form-item>
                <el-form-item style="text-align: center; margin-right: 20px;">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import  {validateCaptcode, validateUsername}  from '@/utils/validate'
import axios from 'axios';

export default {
    data() {
        return {
            ruleForm: {
                username: '',
                password: '',
                captchcode: ''
            },
            rules: {
                username:[
                    {  required: true, message: '请输入用户名', trigger: 'blur' },
                    { validator: validateUsername, trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
                captchcode: [
                    { required: true, message: '请输入验证码', trigger: 'blur' },
                    { validator: validateCaptcode, trigger: 'blur' }
                ]
            }
        };
    },
    
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                axios.post('/user/login', this.ruleForm).then(res => {
                    if(res.status === 200){
                        this.$router.push({ name: 'layout' });
                        this.$message.success('登录成功')
                        
                    }else{
                        this.$message.error('用户名或密码错误')
                    }
                }).catch(()=> {
                    this.$message.error('登录失败，请稍后再试');
                })
                }else{
                    this.$message.error('请按要求填写正确的信息')
                    return false;
                }
            });
        },
        
        resetForm() {
            this.$router.push({ name: 'register' });
        },
        
    }
}
</script>

<style>
html,
body {
    height: 100%;
    margin: 0;
    /* 确保没有默认的外边距 */
}
.main{
    height: 100%;
    width: 100%;
    background: url("../../assets/login.jpeg") center top;
    position: relative;
    
}
.login-box{
    width: 400px;
    position: absolute;
    background-color: #fff;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    padding-top: 30px;
    padding-right: 30px;
    border-radius: 20px;
}

</style>